package org.feng.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.feng.entity.Resource;
import org.feng.base.entity.Result;
import org.feng.service.ResourceService;

import lombok.AllArgsConstructor;
import org.feng.utils.condition.Condition;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 资源表
 * </p>
 *
 * @author feng
 * @since 2021-11-30
 */
@RestController
@RequestMapping("/system/resource")
@AllArgsConstructor
public class ResourceController {

    private final ResourceService resourceService;


    /**
     * 开关
     */
    @PostMapping("/open")
    public Result<String> open(@RequestBody Resource resource) {
        resourceService.open(resource);
        return Result.success("操作成功");
    }

    /**
     * 列表
     */
    @GetMapping()
    public Result<List<Resource>> list(Resource resource) {
        return Result.success(resourceService.list(Condition.getQueryWrapper(resource)));
    }

    /**
     * 分页列表
     */
    @GetMapping("/page")
    public Result<IPage<Resource>> page(Resource resource) {
        return Result.success(resourceService.page(Condition.getPage(), Condition.getQueryWrapper(resource)));
    }

    /**
     * 详情
     */
    @GetMapping("/query")
    public Result<Resource> detail(@RequestParam Long id) {
        return Result.success(resourceService.getById(id));
    }

    /**
     * 新增
     */
    @PostMapping()
    public Result<Boolean> save(@RequestBody Resource resource) {
        return Result.status(resourceService.save(resource));
    }

    /**
     * 修改
     */
    @PutMapping()
    public Result<Boolean> update(@RequestBody Resource resource) {
        return Result.status(resourceService.updateById(resource));
    }

    /**
     * 删除
     */
    @DeleteMapping()
    public Result<Boolean> delete(@RequestParam String ids) {
        return Result.status(resourceService.removeByIds(Arrays.asList(ids.split(","))));
    }
}

